﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class Vehicles : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            ibAdd.Visible = Roles.GetRolesForUser(User.Identity.Name).Contains("Manager");

            ddlSubsidiary.Items.Add(new ListItem("All", "0"));

            foreach (var subsidiary in SubsidiaryBLL.GetAllSubsidiaries().OrderBy(sub => sub.CityId))
            {
                var city = CityBLL.GetCityById(subsidiary.CityId);
                var item = new ListItem(city.Name, subsidiary.SubsidiaryId.ToString());
                ddlSubsidiary.Items.Add(item);
            }

            LoadGridview();
        }

        private void LoadGridview()
        {
            List<Vehicle> vehicles = VehicleBLL.GetAllVehicles();

            if (ddlSubsidiary.SelectedIndex != 0)
            {
                vehicles = vehicles.Where(v => v.SubsidiaryId == Convert.ToByte(ddlSubsidiary.SelectedValue)).ToList();
            }

            if (ddlAvailability.SelectedIndex != 0)
            {
                vehicles =
                    vehicles.Where(v => v.Availability == Convert.ToBoolean(ddlAvailability.SelectedValue)).ToList();
            }

            gvVehicles.DataSource = vehicles.OrderByDescending(v => v.RegistrationNo).ToList();
            gvVehicles.DataBind();
        }

        protected void gvVehicles_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvVehicles.PageIndex = e.NewPageIndex;
            LoadGridview();
        }

        protected void ibtnSearch_OnClick(object sender, ImageClickEventArgs e)
        {
            var keyword = tbSearch.Text.Trim();
            ddlAvailability.SelectedIndex = 0;
            ddlSubsidiary.SelectedIndex = 0;
            if (string.IsNullOrEmpty(keyword))
            {
                LoadGridview();
            }
            else
            {
                var vehicle = VehicleBLL.GetVehiclesByRegNo(keyword);

                gvVehicles.DataSource = vehicle == null ? null : new List<Vehicle> {vehicle};
                gvVehicles.DataBind();
            }
        }

        protected void ibDelete_OnClick(object sender, ImageClickEventArgs e)
        {
            var regNo = Request.QueryString["RegNo"];
            VehicleBLL.RemoveVehicle(regNo);
            LoadGridview();
        }

        protected void ddlAvailability_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            tbSearch.Text = string.Empty;
            LoadGridview();
        }

        protected void ddlSubsidiary_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            tbSearch.Text = string.Empty;
            LoadGridview();
        }

        protected void gvVehicles_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null || e.Row.Cells[e.Row.Cells.Count - 1].Text == "Action")
                e.Row.Cells[e.Row.Cells.Count - 1].Visible =
                    Roles.GetRolesForUser(User.Identity.Name).Contains("Manager");
        }
    }
}